﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MESCodeFile;
using CodeDynamiksEngine;

namespace mes_tublay
{
    public partial class frmScheduleofValues : Form
    {
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Exclusive);
        CodeScript script;
        DataCache schedvalue = new DataCache("MyValue");
        public frmScheduleofValues()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);
            LoadValueSched();
        }

        void LoadValueSched()
        {
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("VALUESCHED");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_QUERY, "Select code, gross, annumtax from tblscheduleofvalues");

            //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)

            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
        }

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            //Make use of the SafeCall method ot safely obtain result from the codeprocessor
            //As it is run from a separate thread.
            proc.SafeCall(this, delegate
            {
                //Check for errors, if there are errors, show it and return to calling code.
                if (proc.State == ProcessorFlag.ReadyErr ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.Error)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //If there are no errors,
                //Check for the Script's name
                switch (proc.GetLastProcessName())
                {
                    case "VALUESCHED":
                        //Fetch the data.
                        proc.DataFetch(out schedvalue);
                        //Set the active page to export.
                        schedvalue.SetActivePage(0);
                        gridscheduleofvalues.DataSource = schedvalue.ExportPage();
                        break;
                }
            });
        }
    }
}
